package com.dp.system.service.impl;

import java.util.Collections;
import java.util.List;
import com.dp.common.utils.DateUtils;
import com.dp.system.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.dp.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.dp.system.domain.SysAlarmLog;
import com.dp.system.mapper.SysAlarmLogMapper;
import com.dp.system.service.ISysAlarmLogService;

/**
 * 报警记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-10
 */
@Service
public class SysAlarmLogServiceImpl implements ISysAlarmLogService {
    @Autowired
    private SysAlarmLogMapper sysAlarmLogMapper;
    @Autowired
    private MailService mailService;

    /**
     * 查询报警记录
     *
     * @param logId 报警记录主键
     * @return 报警记录
     */
    @Override
    public SysAlarmLog selectSysAlarmLogByLogId(Long logId) {
        return sysAlarmLogMapper.selectSysAlarmLogByLogId(logId);
    }

    /**
     * 查询报警记录列表
     *
     * @param sysAlarmLog 报警记录
     * @return 报警记录
     */
    @Override
    public List<SysAlarmLog> selectSysAlarmLogList(SysAlarmLog sysAlarmLog) {
        return sysAlarmLogMapper.selectSysAlarmLogList(sysAlarmLog);
    }

    /**
     * 新增报警记录
     *
     * @param sysAlarmLog 报警记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysAlarmLog(SysAlarmLog sysAlarmLog) {
        sysAlarmLog.setCreateTime(DateUtils.getNowDate());
        int result = sysAlarmLogMapper.insertSysAlarmLog(sysAlarmLog);
        if (result > 0) {
            String subject = "农业大棚温度报警";
            String content = "报警信息：" + sysAlarmLog.getAlarmMessage() + "\n时间：" + sysAlarmLog.getCreateTime();
            mailService.sendAlarmEmail(subject, content);
        }
        return result;
    }

    /**
     * 修改报警记录
     *
     * @param sysAlarmLog 报警记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysAlarmLog(SysAlarmLog sysAlarmLog) {
        sysAlarmLog.setUpdateTime(DateUtils.getNowDate());
        // 删除旧记录
        sysAlarmLogMapper.deleteSysAlarmLogByRuleId(sysAlarmLog.getLogId());
        // 插入新记录
        return sysAlarmLogMapper.insertSysAlarmLog(sysAlarmLog);
    }

    /**
     * 批量删除报警记录
     *
     * @param logIds 需要删除的报警记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysAlarmLogByLogIds(Long[] logIds) {
        sysAlarmLogMapper.deleteSysAlarmLogByRuleIds(logIds);
        return sysAlarmLogMapper.deleteSysAlarmLogByLogIds(logIds);
    }

    /**
     * 删除报警记录信息
     *
     * @param logId 报警记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysAlarmLogByLogId(Long logId) {
        sysAlarmLogMapper.deleteSysAlarmLogByRuleId(logId);
        return sysAlarmLogMapper.deleteSysAlarmLogByLogId(logId);
    }

    @Override
    public List<Object> getAlarmStatistics() {
        return sysAlarmLogMapper.getAlarmStatistics();
    }

    @Override
    public List<SysAlarmLog> getCurrentAlarmLogs() {
        return sysAlarmLogMapper.selectCurrentAlarmLogs();
    }

    /**
     * 新增报警记录信息
     *
     * @param sysAlarmLog 报警记录对象
     */
    @Override
    public void batchInsertSysAlarmLog(SysAlarmLog sysAlarmLog) {
        List<SysAlarmLog> sysAlarmLogList = sysAlarmLog.getSysAlarmLogList();
        Long ruleId = sysAlarmLog.getRuleId();
        if (StringUtils.isNotNull(sysAlarmLogList)) {
            List<SysAlarmLog> list = new ArrayList<>();
            for (SysAlarmLog alarmLog : sysAlarmLogList) {
                alarmLog.setRuleId(ruleId);
                list.add(alarmLog);
            }
            if (!list.isEmpty()) {
                sysAlarmLogMapper.batchSysAlarmLog(list);
            }
        }
    }
}